package com.example.czxyproduct.web;

import com.example.czxyproduct.mapper.Label1Mapper;
import com.example.czxyproduct.mapper.ProductMapper;
import com.yc.czxy.bean.Label1;
import com.yc.czxy.bean.Product;
import com.yc.czxy.bean.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: chengzixy
 * @description:
 * @author: 刘志成
 * @create: 2021-08-23 13:25
 */
@RestController
@RequestMapping("product")
public class ProductAction {

    @Resource
    ProductMapper productMapper;

    /**
     * 添加商品
     * @param product
     * @return
     */
    @PostMapping("addProduct")
    public Result<Product> addProduct(@RequestBody Product product) {
        int code = productMapper.insert(product);
        if (code > 0) {
            return new Result(code, "添加成功", product);
        } else {
            return new Result(code, "添加失败", product);
        }
    }

    /**
     * 根据pid删除商品
     * @param product
     * @return
     */
    @PostMapping("deleteProductBypid")
    public Result<Product> deleteProductBypid(@RequestBody Product product) {
        int code = productMapper.deleteById(product.getPid());
        if (code > 0) {
            return new Result(code, "删除成功", product);
        } else {
            return new Result(code, "删除失败", product);
        }
    }

    @RequestMapping("queryAll")
    public List<Product> queryAll() {
        List<Product> product = productMapper.selectAll();
        System.out.println(product);
        return product;
    }

    @RequestMapping("queryByPid")
    public List<Product> queryByPid(Integer pid){
        List<Product> product = productMapper.selectByPid(pid);
        System.out.println(product);
        return product;
    }

    @Resource
    Label1Mapper label1Mapper;

    /**
     * 添加标签
     * @param label
     * @return
     */
    @PostMapping("addLabel")
    public Result<Product> addLabel(@RequestBody Label1 label) {
        int code = label1Mapper.insert(label);
        if (code > 0) {
            return new Result(code, "添加成功", label);
        } else {
            return new Result(code, "添加失败", label);
        }
    }

    /**
     * 根据用户id查询所拥有的商品
     * @param uid
     * @return
     */
    @GetMapping("selectProductByUid")
    public List<Product> selectProductByUid(Integer uid){
        List<Product> product = productMapper.selectProductByUid(uid);
        return product;
    }



}
